package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_BEAT_BACK, desc = "反击")
public class CrossBeautyEscortBeatBackCmd extends CrossCommand {

    long userId;
    long dartId;
    long enemyUserId;
    long enemyDartId;
    long enemyServerId;
    List<EscortBeautyDispatch> beautyList;
    List<Integer> buyBeautyList;
    String buyConsume;
    long playerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortBeatBackReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortBeatBackReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        dartId = reqMsg.getDartId();
        enemyUserId = reqMsg.getEnemyUserId();
        enemyDartId = reqMsg.getEnemyDartId();
        enemyServerId = reqMsg.getEnemyServerId();
        buyBeautyList = reqMsg.getBuyBeautyList();
        beautyList = CrossBeautyEscortMgr.parseBeautyDispatch(reqMsg.getBeautyList());
        buyConsume = reqMsg.getBuyConsume();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        //临时取8位区服id
        String userIdStr = String.valueOf(userId);
        long userServerId = Long.valueOf(userIdStr.substring(0, 8));
        CrossEscortPlayerWrap friendPlayer = CrossBeautyEscortMgr.getEscortPlayer(userServerId, userId);
        CrossEscortPlayerWrap player = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        CrossEscortPlayerWrap enemyPlayer = CrossBeautyEscortMgr.getEscortPlayer(enemyServerId, enemyUserId);
        if(friendPlayer == null || player == null || enemyPlayer == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        ret = player.beatBack(enemyPlayer, enemyDartId, friendPlayer, beautyList, buyBeautyList, buyConsume, dartId);
    }

    @Override
    public void sendData() {
        if(ret != 0){
            BeautyEscortProto.CrossBeautyEscortBeatBackRespMsg.Builder respMsg = BeautyEscortProto.CrossBeautyEscortBeatBackRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setWin(false);
            respMsg.addAllBeauty(CrossBeautyEscortMgr.parseBeautyDispatchBuilderList(beautyList));
            respMsg.setBuyConsume(buyConsume);
            respMsg.addAllBuyBeauty(buyBeautyList);
            respMsg.setEnemyUserId(enemyUserId);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_BEAT_BACK_FROM_CROSS, respMsg));
        }
    }
}
